package android.bignerdranch.xiangmu;

import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import androidx.appcompat.app.AppCompatActivity;

import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ClassesScore extends AppCompatActivity {
    private static final String PREFS_NAME ="android.bignerdranch.xiangmu" ;
    private MyHelper myHelper;
    SharedPreferences sharedPreferences;
    SharedPreferences.Editor editor;
    List<Map<String, Object>> lists;
    SQLiteDatabase db;
    SimpleAdapter adapter;
    private Map<String,Object> countryMap = new HashMap<String,Object>();
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.classesscore);
        sharedPreferences = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
        editor = sharedPreferences.edit();
        myHelper = new MyHelper(this);
        db = myHelper.getWritableDatabase();
        lists = new ArrayList<>();
        editor.apply();
        Cursor c = db.query("information", null, null,
                null, null, null, null);
        while(c.moveToNext()){
            Map<String, Object> map = new HashMap<>();
            String name = c.getString(c.getColumnIndex("name"));
            String id = c.getString(c.getColumnIndex("id"));
            String student_class = c.getString(c.getColumnIndex("class"));
            String state =c.getString(c.getColumnIndex("state"));
            map.put("name", "姓名:"+name);
            map.put("id", "学号:"+id);
            map.put("classes", "班级:"+student_class);
            map.put("Button",state);
            System.out.println(map);
            lists.add(map);
        }
        /***int count = sharedPreferences.getInt("count",0);
        lists = new ArrayList<>();
        for (int i = 1; i < count; i++) {
            Map<String, Object> map = new HashMap<>();
            String meg_name = "Student"+i+"name";
            String meg_id = "Student"+i+"id";
            String meg_classes = "Student"+i+"classes";
            map.put("name", "姓名:"+sharedPreferences.getString(meg_name,null));
            map.put("id", "学号:"+sharedPreferences.getString(meg_id,null));
            map.put("classes", "班级:"+sharedPreferences.getString(meg_classes,null));
            map.put("Button","已到");
            lists.add(map);
        }***/
        adapter = new SimpleAdapter(ClassesScore.this, lists, R.layout.listed, new String[]{"name", "id", "classes","Button"}, new int[]{R.id.text1, R.id.text2, R.id.text3,R.id.is_listened});
        ListView listView = (ListView) findViewById(R.id.stu_list_saved);
        listView.setAdapter(adapter);
    }
}
